package hotel.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import hotel.entity.Authority;
import hotel.entity.Role;
import hotel.entity.User;
import hotel.service.IAuthorityService;
import hotel.service.IRoleService;
import hotel.service.IUserService;
import util.JSONUtil;

@Controller
public class RoleController {

	//Jackson
	private final static ObjectMapper mapper = new ObjectMapper();
	@Autowired
	private IRoleService<Role> roleService;
	@Autowired
	private IAuthorityService<Authority> authorityService;
	
	@RequestMapping(value="roleManagement")
	public String roleManagement() {
		
		return "role/rolelist";
	}
		
	/**
	 * 返回JSON格式的角色数据供DataTables使用
	 * */
	@ResponseBody
	@RequestMapping(value = "rolelist")
	public String getUserList(Model model) {
		String json=null;
		try {
			mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
			json=mapper.writeValueAsString(roleService.listAll("Role"));
			json="{\"data\":"+json+"}";
		} catch (IOException e) {
			e.printStackTrace();
		}
		return json;
	}
	
	@RequestMapping(value="addRole",method=RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute("auths",authorityService.findAllAuthority());
		return "role/add";
	}
	
	@ResponseBody
	@RequestMapping(value="addRole",method=RequestMethod.POST)
	public String addRole(Role role,String[] auths) {
		List<Authority> set=new ArrayList<>();
		for(String str:auths) {
			Authority authority=new Authority();
			authority.setAuthorityid(Integer.parseInt(str));
			set.add(authority);
		}
		role.setAuthoritys(set);
		role.setCreatedTime(new Date());
		if(roleService.save(role))
			return JSONUtil.returnSuccess();
		else
			return JSONUtil.returnFailure();
	}
	
	@RequestMapping(value="editRole",method=RequestMethod.GET)
	public String edit(int roleid,Model model) {
		model.addAttribute("auths",authorityService.findAllAuthority());
		model.addAttribute("role", roleService.findById(Role.class, roleid));
		return "role/edit";
	}
	
	@ResponseBody
	@RequestMapping(value="editRole",method=RequestMethod.POST)
	public String editRole(Role role,String[] auths) {
		List<Authority> set=new ArrayList<>();
		for(String str:auths) {
			Authority authority=new Authority();
			authority.setAuthorityid(Integer.parseInt(str));
			set.add(authority);
		}
		role.setAuthoritys(set);
		if(roleService.update(role))
			return JSONUtil.returnSuccess();
		return JSONUtil.returnFailure();
	}
	
	@RequestMapping(value="viewRole",method=RequestMethod.GET)
	public String view(int roleid,Model model) {
		model.addAttribute("auths",authorityService.findAllAuthority());
		model.addAttribute("role", roleService.findById(Role.class, roleid));
		return "role/view";
	}
	
}
